package com.yiyi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yiyi.entity.Answer;
import com.yiyi.form.AddAnswerForm;
import com.yiyi.form.SearchQuestionForm;
import com.yiyi.mapper.AnswerMapper;
import com.yiyi.service.AnswerService;
import com.yiyi.vo.AnswerPageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author yiyi
 * @date 2021/5/31
 */
@Service
public class AnswerServiceImpl implements AnswerService {

    @Autowired
    private AnswerMapper answerMapper;

    /**
     * 保存新题目
     *
     * @param answerForm
     * @return
     * */
    @Override
    public int save(AddAnswerForm answerForm) {
        Answer answer = new Answer();

        answer.setAnswerId(String.valueOf(System.currentTimeMillis()));
        answer.setAnswerGrade(answerForm.getAnswerGrade());
        answer.setAnswerTitle(answerForm.getAnswerTitle());
        answer.setAnswerKey(answerForm.getAnswerKey());
        answer.setAnswerTag(answerForm.getAnswerTag().toString());
        // ****************************************************************************
        // TODO 需调用information微服务获取该员工对应的公司id并保存至其中
        answer.setAnswerCompanyId("1111111");
        // ****************************************************************************
        return answerMapper.insert(answer);
    }

    /**
     * 删除题目
     *
     * @param id
     * @return
     * */
    @Override
    public int delete(String id) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("answer_id", id);
        return answerMapper.delete(queryWrapper);
    }

    /**
     * 分页搜索及查询
     *
     * @param searchQuestionForm
     * @return
     * */
    @Override
    public AnswerPageVO findByPage(SearchQuestionForm searchQuestionForm) {
        AnswerPageVO answerPageVO = new AnswerPageVO();

        QueryWrapper queryWrapper = new QueryWrapper();
        if (searchQuestionForm.getTitle() != null) {
            queryWrapper.like("answer_title", searchQuestionForm.getTitle());
        }
        if (searchQuestionForm.getTag() != null) {
            queryWrapper.like("answer_tag", searchQuestionForm.getTag());
        }
        IPage<Answer> page = new Page<>(searchQuestionForm.getCurrent(), searchQuestionForm.getSize());
        page = answerMapper.selectPage(page, queryWrapper);

        answerPageVO.setCurrent(searchQuestionForm.getCurrent());
        answerPageVO.setSize(searchQuestionForm.getSize());
        answerPageVO.setPages(page.getPages());
        answerPageVO.setTotal(page.getTotal());
        answerPageVO.setAnswers(page.getRecords());
        return answerPageVO;
    }

    /**
     * 根据公司id查询题目列表
     *
     * @param companyId
     * @return
     * */
    @Override
    public List<Answer> findByCompanyId(String companyId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("answer_company_id", companyId);
        return answerMapper.selectList(queryWrapper);
    }

    /**
     * 根据题目id查询单道题目
     *
     * @param id
     * @return
     * */
    @Override
    public Answer findOneById(String id) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("answer_id", id);
        return answerMapper.selectOne(queryWrapper);
    }

    /**
     * 根据id列表查询题目列表
     *
     * @param ids
     * @return
     * */
    @Override
    public List<Answer> findListByIds(List<String> ids) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.in("answer_id", ids);
        return answerMapper.selectList(queryWrapper);
    }
}
